/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 *
 */

import { Json5Reader } from '@ohos/hvigor';
import fse from 'fs-extra';
import path from 'path';

import { CommonConst } from '../const/common-const.js';

/**
 * 判断当前插件是否是单框架版本
 *
 * @return {boolean} true/false
 */
export function isSingleFramework(): boolean {
  const frameworkConfigurationPath = path.resolve(__dirname, '../../res', CommonConst.FRAMEWORK_CONFIGURATION);
  if (fse.existsSync(frameworkConfigurationPath)) {
    const pkgJsonObj = Json5Reader.getJson5Obj(frameworkConfigurationPath);
    return pkgJsonObj.frameworkType !== CommonConst.DOUBLE_FRAMEWORK;
  }
  return true;
}

/**
 * 解析$profile形式的配置文件名称
 * ($profile:form_config解析为form_config)
 *
 * @return {string} 返回解析后的名称
 */
export function parsingProfileName(srcStr: string): string | undefined {
  const reg = /^[$]profile:[0-9a-zA-Z_.]+|(?=.*[{])(?=.*[}])[0-9a-zA-Z_.{}]+$/;
  if (!reg.test(srcStr)) {
    return undefined;
  }
  return srcStr.split(':')[1];
}
